define("module/comment",function(require, exports, module){
	var $ = require('lib/jquery'),
		Ajax = require('util/ajax'),
		Cgi = require('module/cgi'),
		undefined;
	var Comment = module.exports = {};
	var sendRequestQuene = [], isProcess = false,
		urlMap = {'get':{'url':Cgi.get('getComment'), type:'get'},
				'del':{'url':Cgi.get('getComment'), type:'post'},
				'add':{'url':Cgi.get('getComment'), type:'post'}};
	function sendRequest(type, params, callback){
		if (isProcess) {
			sendRequestQuene.push(function(){
				sendRequest(type, params, callback);
			});
			return;
		};
		isProcess = true;
		var url = urlMap[type].url, method = urlMap[type].type;
		params = $.extend({}, params || {});
		Ajax[method]({
			'url': url,
			'data': params,
			'success': function(res){
				if (res.success) {
					callback && callback(0, res);
				}else{
					callback && callback(-1);
				}
				isProcess = false;
				var func = sendRequestQuene.shift();
				typeof func == 'function' && func();
			},
			'fail': function(){
				callback && callback(-500);
				isProcess = false;
				var func = sendRequestQuene.shift();
				typeof func == 'function' && func();
			}
		});
	}
	Comment.get = function(params, callback, isAll){
		sendRequest('get', params, callback);
	}
	Comment.del = function(params, callback){
		sendRequest('del', params, callback);
	}
	Comment.add = function(params, callback){
		sendRequest('add', params, callback);
	}
});


define("ui/widget/comment",function(require, exports, module){
	var $ = require('lib/jquery'),
		CommentModule = require('module/comment'),
		Cgi = require('module/cgi'),
		Util = require('module/util'),
		Ajax = require('util/ajax'),
		Dialog = require('ui/widget/dialog'),
		template = require('ui/template'),
		undefined;

	function Comment(container, params){
		this.container = container;
		this.params = params || {};
		this.isGlobalGapTime = params.isGlobalGapTime || true;//是否全局控制两次间隔时间
		this.init();
	}
	Comment.template =
			'<div class="d-comment-list j_comment_list_container">\
				<% for(var i=0,l=comments.length; i<l; i++){ %>\
				<div class="d-comment-li j_comment_li">\
					<div class="d-c-user-image"><img src=<%= getImageUrl(comments[i].user.image) %> /></div>\
					<div class="d-c-comment-info">\
						<div class="d-c-user-info">\
							<div class="d-c-user-name j_comment_user"><span class="j_comment_user_name"><%=escapeHTML(comments[i].user.nickname)%></span><span class="c-time"><%=comments[i].time%></span></div>\
							<% if (user.is_admin || (user && (user.id == comments[i].user.id || user.id == datas.uid))) { %>\
							<div class="d-c-user-replay j_comment_delete" d-cid="<%=comments[i].id%>" style="margin-left:20px;"><a href="javascript:;">删除</a></div>\
							<% } %>\
							<div class="clear"></div>\
						</div>\
						<div class="d-c-comment-text">\
							<%=escapeHTML(comments[i].content)%>\
							<% if (comments[i].target_comment) { %>\
								<span>@<%=escapeHTML(comments[i].target_comment.user.nickname)%></span>\
							<% } %>\
						</div>\
					</div>\
					<div class="clear"></div>\
				</div>\
				<% } %>\
			</div>\
			<div class="d-comment-more j_comment_more"<% if (datas.isAll) { %> style="display:none;"<%}%>><div class="d-more-btn">查看更多</div></div>';
	Comment.prototype = {
		init: function(){
			this.getData();
		},
		getData: function(){
			var _this = this;
			var id = this.params.id;
			CommentModule.get({'discovery_id':id}, function(status, response){
				if (status == 0) {
					response.user.is_admin = window.is_admin;
					_this.parseList(response);
					_this.bindEvent();
				};
			});
		},
		parseList: function(commentData, isAll){
			isAll = isAll || true;
			this.params.isAll = isAll;
			var html = template.parse(Comment.template, {comments:commentData.comments, user:commentData.user, datas:this.params, escapeHTML:Util.escapeHTML, getImageUrl: Util.getImageUrl});
			$(this.container).html(html);
		},
		appendItem: function(commentData){
			var html = '<div class="d-comment-li j_comment_li">\
					<div class="d-c-user-image"><img src=<%= getImageUrl(comment.user.image) %> /></div>\
					<div class="d-c-comment-info">\
						<div class="d-c-user-info">\
							<div class="d-c-user-name j_comment_user"><span class="j_comment_user_name"><%=escapeHTML(comment.user.nickname)%></span><span class="c-time"><%=comment.time%></span></div>\
							<% if (user.is_admin || (user && (user.id == comment.user.id || user.id == datas.uid))) { %>\
							<div class="d-c-user-replay j_comment_delete" d-cid="<%=comment.id%>" style="margin-left:20px;"><a href="javascript:;">删除</a></div>\
							<% } %>\
							<div class="clear"></div>\
						</div>\
						<div class="d-c-comment-text">\
							<%=escapeHTML(comment.content)%>\
							<% if (comment.target_comment) { %>\
								<span>@<%=escapeHTML(comment.target_comment.user.nickname)%></span>\
							<% } %>\
						</div>\
					</div>\
					<div class="clear"></div>\
				</div>';
			var html = template.parse(html, {comment:commentData.comment, user:commentData.user, datas:this.params, escapeHTML:Util.escapeHTML, getImageUrl: Util.getImageUrl});
			$('.j_comment_list_container').append(html);
		},
		bindEvent: function(){
			var _this = this;
			$('.j_comment_value').bind('paste', function(){return false});
			// $('.j_submit_comment').bind('click', function(){
			// 	var curTime = +new Date(), isLegal = true;
			// 	if (_this.isGlobalGapTime) {
			// 		if (window.__lastSubmitCommentTime && curTime - window.__lastSubmitCommentTime < 60*5000) {
			// 			isLegal = false;
			// 		};
			// 	}else{
			// 		if (_this.lastSubmitTime && curTime - _this.lastSubmitTime < 60*5000) {
			// 			isLegal = false;
			// 		};
			// 	}
			// 	if (!isLegal) {
			// 		Dialog.notice({'content':'两次评论的时间至少要间隔5分钟哦！'});
			// 		return false;
			// 	};
			// 	var text = $.trim($('.j_comment_value').val());
			// 	if (!text) {
			// 		Dialog.notice({'content': '评论内容不能为空！'});
			// 	}else{
			// 		var data = {'discovery_id':_this.params.id, 'content':text},
			// 			target_id = $('.j_comment_value')[0].target_id;
			// 		if (target_id) {
			// 			data.target_id = target_id;
			// 		};
			// 		Ajax.post({
			// 			url: Cgi.get('saveComment'),
			// 			data: data,
			// 			success: function(response){
			// 				if (response.success) {
			// 					response.user.is_admin = window.is_admin;
			// 					_this.appendItem(response);
			// 					$('.j_comment_value').val('');
			// 					$('.j_comment_value').attr('placeholder', '添加一个评论...');
			// 					$('.j_comment_value')[0].target_id = null;
			// 					$('.j_comment_value').blur();
			// 					_this.lastSubmitTime = window.__lastSubmitCommentTime = +new Date();
			// 				}else{
			// 					Dialog.notice({'content': response.msg});
			// 				}
			// 			}
			// 		});
			// 	}
			// 	return false;
			// });
			$('.j_comment_list_container').delegate('.j_comment_delete', 'click', function(){
				var that = this;
				Dialog.confirm({
					'content':'确认删除这条评论吗?',
					'onSubmitted':function(){
						var cId = $(that).attr('d-cid');
						Ajax.post({
							url: Cgi.get('deleteComment'),
							data: {'discovery_id':_this.params.id, 'comment_id':cId},
							success: function(response){
								if (response.success) {
									$(that).parents('.j_comment_li').remove();
								}else{
									Dialog.notice({'content': response.msg});
								}
							}
						});
					}
				});
				return false;
			});
			// $('.j_comment_list_container').delegate('.j_comment_reply', 'click', function(){
			// 	var cId = $(this).attr('d-cid');
			// 	$('.j_comment_value')[0].target_id = cId;
			// 	$('.j_comment_value').attr('placeholder', '回复'+$(this).siblings('.j_comment_user').find('.j_comment_user_name').text()+':');
			// 	$('.j_comment_value').focus();
			// 	return false;
			// });
		},
		destroy: function(){
			// $('.j_submit_comment').unbind('click');
			$('.j_comment_list_container').undelegate('.j_comment_delete', 'click');
			// $('.j_comment_list_container').undelegate('.j_comment_reply', 'click');
		}
	}
	module.exports = Comment;
});
